package com.ebrothers.isns.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.ebrothers.isns.common.Utils;
import com.ebrothers.isns.vo.User;

public class UserDAO extends GenericDAO {

	public long insertUser(User user) throws DAOException {
		String sql = "insert into users (name) values ('" + user.getName()
				+ "');";
		insert(sql);
		return getIdByUserName(user);
	}

	public User getUser(long id) throws DAOException {
		String sql = "select * from users where id=" + id;
		ResultSet rs = query(sql);
		User user = null;
		try {
			if (rs.next()) {
				user = new User();
				user.setId(rs.getLong("id"));
				user.setName(Utils.encodeText(rs.getString("name")));
			}
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			closeResultSet(rs);
		}
		return user;
	}

	public long getIdByUserName(User user) throws DAOException {
		return getIdByIdentify("users", "name", user.getName());
	}

	public boolean isUserExist(User user) throws DAOException {
		return super.isExist("users", "name", user.getName());
	}
}
